﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RentalSystem.Data.EF.Core;
using Web.Context;
using RentalSystem.Data.EF;

namespace Web.Helper
{
    public class SettingsManager
    {
        protected IDataContext dataContext;
        private Dictionary<string, string> settings;

        public Dictionary<string, string> Settings
        {
            get
            {
                if(this.settings == null)
                {
                    settings = dataContext.SettingsDao.AllSettings();
                }

                return this.settings;
            }
        }

        public SettingsManager()
        {
            this.dataContext = CurrentContext.Context.DataContext;
        }

        public int BigItemsCountOnPages
        {
            get
            {
                return int.Parse(Settings["BigItemsCountOnPages"]);
            }
            set
            {
                SaveSetting("BigItemsCountOnPages", value.ToString());
            }
        }
        public int SmallItemsCountOnPages
        {
            get
            {
                return int.Parse(Settings["SmallItemsCountOnPages"]);
            }
            set
            {
                SaveSetting("SmallItemsCountOnPages", value.ToString());
            }
        }
        public float HirePricePerDay
        {
            get
            {
                return int.Parse(Settings["HirePricePerDay"]);
            }
            set
            {
                SaveSetting("HirePricePerDay", value.ToString());
            }
        }
        public int HiresCountPerOneCustomer
        {
            get
            {
                return int.Parse(Settings["HiresCountPerOneCustomer"]);
            }
            set
            {
                SaveSetting("HiresCountPerOneCustomer", value.ToString());
            }
        }
        public int HireTimeInDay
        {
            get
            {
                return int.Parse(Settings["HireTimeInDay"]);
            }
            set
            {
                SaveSetting("HireTimeInDay", value.ToString());
            }
        }
        public float HireLateRewardPerDay
        {
            get
            {
                return int.Parse(Settings["HireLateRewardPerDay"]);
            }
            set
            {
                SaveSetting("HireLateRewardPerDay", value.ToString());
            }
        }
        public int HiresCountToNextLevel
        {
            get
            {
                return int.Parse(Settings["HiresCountToNextLevel"]);
            }
            set
            {
                SaveSetting("HiresCountToNextLevel", value.ToString());
            }
        }
        public int UserDiscountPerLevelInPercentage
        {
            get
            {
                return int.Parse(Settings["UserDiscountPerLevelInPercentage"]);
            }
            set
            {
                SaveSetting("UserDiscountPerLevelInPercentage", value.ToString());
            }
        }
                        
        private void SaveSetting(string key, string value)
        {
            if (key == null || value == null)
                throw new ArgumentNullException();

            dataContext.SettingsDao.Update(new Settings() { SettingKey = key, SettingValue = value }, 0);
            this.settings = null;
        }
    }
}